package com.baike.controller;


import com.baike.data.Result;
import com.baike.em.ConstantEnum;
import com.baike.entity.User;
import com.baike.service.IUserService;
import com.baike.util.MD5Utils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author x_FG
 * @since 2021-10-29
 */
@RestController
@RequestMapping("/user")
@RefreshScope
@Api(value = "运营用户查询接口")
public class UserController {

    @Autowired
    private IUserService iUserService;

    @ApiOperation("运营用户查询方法")
    @GetMapping
    public Result getUserInfo(){
        List<User> list = iUserService.list();
        return Result.build(ConstantEnum.SUCCESS).setResult(list);
    }

    @ApiOperation("运营用户增加方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user",value = "实体类user",required = true,paramType = "body"),
            @ApiImplicitParam(name = "roleIds",value = "role主键id",required = true,paramType = "path")
    })
    @PostMapping("/{roleIds}")
    public Result insertUserInfo(@RequestBody User user,@PathVariable("roleIds")Integer[] roleIds) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        boolean b = iUserService.insertUserInfo(user,roleIds);
        if (b){
            return Result.build(ConstantEnum.SUCCESS);
        }
        return Result.build(ConstantEnum.ERROR);
    }

    @ApiOperation("运营用户根据Id查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid",value = "user实体类主键Id",required = true,paramType = "path")
    })
    @GetMapping("u/{uid}")
    public Result getUserById(@PathVariable("uid")Integer uid){
        User user = iUserService.getUserById(uid);
        return Result.build(ConstantEnum.SUCCESS).setResult(user);
    }

    @ApiOperation("运营用户修改方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user",value = "user实体类",required = true,paramType = "body"),
            @ApiImplicitParam(name = "roleIds",value = "role主键id",required = true,paramType = "path")
    })
    @PutMapping("/{roleIds}")
    public Result updateUserInfo(@RequestBody User user,@PathVariable("roleIds")Integer[] roleIds) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        if (user.getUid() == null){
            return Result.build(ConstantEnum.ERROR).setResult("请确认用户id");
        }
        user.setPwd(MD5Utils.getEncryptedPwd(user.getPwd()));
        boolean b = iUserService.updateUserInfo(user,roleIds);
        if (b){
            return Result.build(ConstantEnum.SUCCESS);
        }
        return Result.build(ConstantEnum.ERROR);
    }

    @ApiOperation("运营用户删除方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid",value = "user实体类主键Id",required = true,paramType = "path")
    })
    @DeleteMapping("/{uid}")
    public Result deleteUserById(@PathVariable("uid")Integer uid){
        boolean b = iUserService.deleteUserById(uid);
        if (b){
            return Result.build(ConstantEnum.SUCCESS);
        }
        return Result.build(ConstantEnum.ERROR);
    }

}
